package cn.cangfei.massage.service.impl;

import cn.cangfei.massage.dto.param.MassageProductCategoryParam;
import cn.cangfei.massage.entity.MassageProduct;
import cn.cangfei.massage.entity.MassageProductCategory;
import cn.cangfei.massage.exception.YyghException;
import cn.cangfei.massage.mapper.MassageProductCategoryMapper;
import cn.cangfei.massage.service.IMassageProductService;
import cn.cangfei.massage.service.IMassageProductCategoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MassageProductCategoryServiceImpl extends ServiceImpl<MassageProductCategoryMapper, MassageProductCategory> implements IMassageProductCategoryService {

    @Resource
    private IMassageProductService massageProductService;

    /**
     * 判断分类名称是否存在
     *
     * @param name
     * @return
     */
    @Override
    public boolean countByName(String name) {
        LambdaQueryWrapper<MassageProductCategory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MassageProductCategory::getName, name);
        int count = count(lambdaQueryWrapper);
        return count > 0;
    }

    /**
     * 添加分类
     *
     * @param param
     * @return
     */
    @Override
    public boolean insertProductCategory(MassageProductCategoryParam param) {
        // 添加
        MassageProductCategory category = new MassageProductCategory();
        BeanUtils.copyProperties(param, category);
        return save(category);
    }

    /**
     * 修改分类
     *
     * @param id
     * @param param
     * @return
     */
    @Transactional
    @Override
    public boolean updateProductCategory(Long id, MassageProductCategoryParam param) {

        MassageProductCategory category = getById(id);
        String name = category.getName();
        if (!name.equals(param.getName())) {
            LambdaQueryWrapper<MassageProductCategory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(MassageProductCategory::getName, param.getName());
            int count = count(lambdaQueryWrapper);
            if (count > 0) {
                throw new YyghException(500, "该分类名称已经存在");
            }
        }

        MassageProductCategory massageProductCategory = new MassageProductCategory();
        BeanUtils.copyProperties(param, massageProductCategory);


        // 修改产品表里面的
        LambdaQueryWrapper<MassageProductCategory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MassageProductCategory::getId, id);
        boolean update = update(massageProductCategory, lambdaQueryWrapper);

        // 修改产品表里面的分类名称


        // 修改就诊产品表里面的分类名称
        // 在修改之前先判断是否有这个产品是这个分类
        LambdaQueryWrapper<MassageProduct> massageProductLambdaQueryWrapper = new LambdaQueryWrapper<>();
        massageProductLambdaQueryWrapper.eq(MassageProduct::getCategoryId, id);
        int count = massageProductService.count(massageProductLambdaQueryWrapper);
        boolean update1 = true;
        if (count > 0) {
            // 有的话才去修改
            LambdaUpdateWrapper<MassageProduct> visitProductLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            visitProductLambdaUpdateWrapper.eq(MassageProduct::getCategoryId, id);

            visitProductLambdaUpdateWrapper.set(MassageProduct::getCategoryName, param.getName());

            update1 = massageProductService.update(visitProductLambdaUpdateWrapper);
        }

        return update && update1;

    }

    /**
     * 查询所有分类
     *
     * @return
     */
    @Override
    public List<MassageProductCategory> selectAllProductCategory() {
        return list();
    }

    /**
     * 分页：根据分类名称，获取分类列表
     *
     * @param keyword  分类名称
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public IPage<MassageProductCategory> selectProductCategoryByPage(String keyword, Integer pageNum, Integer pageSize) {
        IPage<MassageProductCategory> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<MassageProductCategory> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        if (keyword != null) {
            lambdaQueryWrapper.like(MassageProductCategory::getName, keyword);
        }
        return page(page, lambdaQueryWrapper);
    }

    /**
     * 根据分类名称，查询分类
     *
     * @param id
     * @return
     */
    @Override
    public boolean deleteProductCategory(Long id) {

        // 需要去商品表里面看是否有这个分类的商品
        LambdaQueryWrapper<MassageProduct> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MassageProduct::getCategoryId, id);

        int count = massageProductService.count(lambdaQueryWrapper);
        if (count > 0) {
            throw new YyghException(500, "该分类下有商品，不能删除");
        }
        // 下面就表示能删除
        return removeById(id);
    }

    /**
     * 根据分类id查询是否存在
     *
     * @param id
     * @return
     */
    @Override
    public boolean countById(Long id) {
        LambdaQueryWrapper<MassageProductCategory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MassageProductCategory::getId, id);
        int count = count(lambdaQueryWrapper);
        return count > 0;
    }

    /**
     * 批量删除分类
     *
     * @param ids
     * @return
     */
    @Transactional
    @Override
    public boolean deleteProductCategories(List<Long> ids) {
        ids.forEach(this::deleteProductCategory);
        return true;
    }

    /**
     * 根据分类id，查询分类
     *
     * @param id
     * @return
     */
    @Override
    public MassageProductCategory selectProductCategoryById(Long id) {
        LambdaQueryWrapper<MassageProductCategory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MassageProductCategory::getId, id);
        MassageProductCategory category = getOne(lambdaQueryWrapper);
        return category;
    }
}
